package com.bawei.module_open

import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import com.bawei.lib_common.mvi.base.BaseMVIFragment
import com.bawei.module_open.databinding.FragmentOpenBinding
import com.bawei.module_open.qi.ui.VideoRecordActivity


class OpenFragment :BaseMVIFragment() {
    private val binding: FragmentOpenBinding by lazy {
        FragmentOpenBinding.inflate(layoutInflater)
    }
    companion object {
        private val REQUIRED_PERMISSIONS = arrayOf(
            Manifest.permission.CAMERA,
            Manifest.permission.RECORD_AUDIO,
            Manifest.permission.WRITE_EXTERNAL_STORAGE, // Android 10 及以下需要
            Manifest.permission.READ_EXTERNAL_STORAGE    // Android 10 及以下需要
        )
    }
    override fun generateViewModel() {

    }

    override fun lazyLoad() {

    }

    override fun getFragmentRootView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        return binding.root
    }

    override fun handleState() {

    }

    override fun initView() {
        super.initView()

        
        // 设置摄像头按钮点击事件
        binding.kaipai.setOnClickListener {
            if (checkAndRequestAllPermissions()){
                startVideoRecording()
            }

        }
        
        // 设置草稿箱点击事件
        binding.root.findViewById<View>(R.id.bg).setOnClickListener {
            startVideoRecording()
        }
    }
    
    /**
     * 启动视频录制页面
     */
    private fun startVideoRecording() {
        val intent = Intent(requireContext(), VideoRecordActivity::class.java)
        startActivity(intent)
    }
    private fun checkAndRequestAllPermissions(): Boolean {
        val permissions = mutableListOf(
            Manifest.permission.CAMERA,
            Manifest.permission.RECORD_AUDIO
        )
        // Android 10 及以下额外申请存储权限
        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.Q) {
            permissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE)
            permissions.add(Manifest.permission.READ_EXTERNAL_STORAGE)
        }

        // 过滤未授权的权限
        val notGranted = permissions.filter {
            ContextCompat.checkSelfPermission(requireActivity(), it) != PackageManager.PERMISSION_GRANTED
        }

        if (notGranted.isNotEmpty()) {
            ActivityCompat.requestPermissions(requireActivity(), notGranted.toTypedArray(), 100)
            return false
        }
        return true
    }
    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        if (requestCode == 100) {
            if (grantResults.isNotEmpty() && grantResults.all { it == PackageManager.PERMISSION_GRANTED }) {
//                initVideoRecording() // 权限全部授予后初始化录制功能
            } else {
                Toast.makeText(requireActivity(), "需要相机和录音权限才能录制视频，请全部授权！", Toast.LENGTH_LONG).show()
            }
        }
    }

}